package cn.uniquetech.util;

import java.util.List;

public class GenXMLUtil {

	private static String xmlStr = "";
	private static String headerStr = "\t";
	private static String tailStr = "\r\n";

	public static String getXmlStr(String tableName, String packageName, List<String> colNames) {
		String servicePackage = packageName + ".entity.sqlmap";
		String capName = StrUtil.initCap(tableName);
		capName = capName.substring(0, capName.length() - 1);
		String entityName = packageName + ".entity." + capName + "Entity";
		xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + tailStr;
		xmlStr += "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">" + tailStr;
		xmlStr += "<mapper namespace=\"" + capName + "Mapper\">" + tailStr;

		// getItems
		xmlStr += headerStr + "<!-- 查询实体集合 -->" + tailStr;
		xmlStr += headerStr + "<select id=\"getItems\" parameterType=\"" + entityName + "\" resultType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " select * from " + tableName + tailStr;
		xmlStr += headerStr + "</select>" + tailStr;

		// insert
		xmlStr += headerStr + "<!-- 插入实体 -->" + tailStr;
		xmlStr += headerStr + "<insert id=\"newItem\" parameterType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " " + genInsertSqlStr(tableName, colNames) + tailStr;
		xmlStr += headerStr + "</insert>" + tailStr;

		// update
		xmlStr += headerStr + "<!-- 更新实体 -->" + tailStr;
		xmlStr += headerStr + "<update id=\"editItem\" parameterType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " " + genUpdateSqlStr(tableName, colNames) + tailStr;
		xmlStr += headerStr + "</update>" + tailStr;

		// remove by entity
		xmlStr += headerStr + "<!-- 删除一个实体 -->" + tailStr;
		xmlStr += headerStr + "<delete id=\"removeItem\" parameterType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " delete from " + tableName + " where iid = #{iid}" + tailStr;
		xmlStr += headerStr + "</delete>" + tailStr;

		// remove by iid
		xmlStr += headerStr + "<!-- 删除一个实体 -->" + tailStr;
		xmlStr += headerStr + "<delete id=\"removeItemById\" parameterType=\"int\">" + tailStr;
		xmlStr += headerStr + " delete from " + tableName + " where iid = #{_parameter}" + tailStr;
		xmlStr += headerStr + "</delete>" + tailStr;

		// remove by iids
		xmlStr += headerStr + "<!-- 删除多个实体 -->" + tailStr;
		xmlStr += headerStr + "<delete id=\"removeItems\" parameterType=\"string\">" + tailStr;
		xmlStr += headerStr + " delete from " + tableName + " where iid in (${_parameter})" + tailStr;
		xmlStr += headerStr + "</delete>" + tailStr;

		// getOneItem
		xmlStr += headerStr + "<!-- 获取单个实体 -->" + tailStr;
		xmlStr += headerStr + "<select id=\"fecthOneEntity\" parameterType=\"" + entityName + "\" resultType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " select * from " + tableName + " where iid = #{iid}" + tailStr;
		xmlStr += headerStr + "</select>" + tailStr;

		// 分页查询
		xmlStr += headerStr + "<!-- 分页查询 -->" + tailStr;
		xmlStr += headerStr + "<select id=\"queryDataGrid\" parameterType=\"" + entityName + "\" resultType=\"" + entityName + "\">" + tailStr;
		xmlStr += headerStr + " select * from " + tableName + " where 1=1 order by iid desc limit #{start},#{rows} " + tailStr;
		xmlStr += headerStr + "</select>" + tailStr;

		xmlStr += headerStr + "<select id=\"queryDataGrid_count\" parameterType=\"" + entityName + "\" resultType=\"int\">" + tailStr;
		xmlStr += headerStr + " select count(*) from " + tableName + tailStr;
		xmlStr += headerStr + "</select>" + tailStr;

		xmlStr += "</mapper>";
		FileUtil.writeFile(servicePackage, capName + ".xml", xmlStr);
		return xmlStr;
	}

	public static String genInsertSqlStr(String tableName, List<String> colNames) {
		String sqlStr = "insert into " + tableName + " (";
		String fields = "";
		String values = "";

		for (String colName : colNames) {
			if ("iid".equals(colName)) {
				continue;
			}
			fields += colName + ",";
			values += "#{" + colName + "},";
		}

		fields = fields.substring(0, fields.length() - 1);
		values = values.substring(0, values.length() - 1);
		sqlStr += fields + ") " + tailStr;
		sqlStr += headerStr + " values " + tailStr;
		sqlStr += headerStr + "(" + values + ")";
		return sqlStr;
	}

	public static String genUpdateSqlStr(String tableName, List<String> colNames) {
		String sqlStr = "update " + tableName + " set ";

		String fields = "";

		for (String colName : colNames) {
			if ("iid".equals(colName) || "adduid".equals(colName) || "adduname".equals(colName) || "adddate".equals(colName)) {
				continue;
			}
			fields += colName + "=#{" + colName + "},";
		}

		fields = fields.substring(0, fields.length() - 1);
		sqlStr += fields + "\r\n";
		sqlStr += headerStr + " where iid =#{iid}";
		return sqlStr;
	}

	public static void main(String[] args) {
		System.out.println(getXmlStr("test", "cn.uniquetech", null));
	}
}
